package com.example.yubo.zyfypt_403yb.model;

import com.example.yubo.zyfypt_403yb.bean.ArticleBean;
import com.example.yubo.zyfypt_403yb.bean.TwareBean;
import com.example.yubo.zyfypt_403yb.iface.ArticleListener;
import com.example.yubo.zyfypt_403yb.iface.TwareIface;
import com.example.yubo.zyfypt_403yb.iface.TwareListener;
import com.example.yubo.zyfypt_403yb.service.ArticleService;
import com.example.yubo.zyfypt_403yb.service.TwareService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by yubo on 2017/5/9.
 */

public class TwareModel implements TwareIface{
    private Retrofit retrofit;
    private String BASEURL="http://amicool.neusoft.edu.cn/";
    //构造函数
    public TwareModel(){
        retrofit = new Retrofit.Builder()
                .baseUrl(BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    //获取课件数据
    public void getResoutList(String mod, int page, String sessionID, final TwareListener twareListener) {
        //使用retrofit----2
        TwareService twareService = retrofit.create(TwareService.class);

        Call<List<TwareBean>> call=twareService.getResult(mod,page,sessionID);
        //使用retrofit ---3
        call.enqueue(new Callback<List<TwareBean>>() {
            @Override
            public void onResponse(Call<List<TwareBean>> call, Response<List<TwareBean>> response) {
                if(response.isSuccessful()&& response!=null){

                    twareListener.onResponse(response.body());
                }else{
                    twareListener.onFail("onresponse fail");
                }
            }

            @Override
            public void onFailure(Call<List<TwareBean>> call, Throwable t) {
                twareListener.onFail(t.toString());
                //写完 然后在mainActivity 里边调用就可以
                //onFailure 网没联通
                //onResponse里边错误 说明拿到的数据可能是空的
            }
        });
        ;   }
}


